package com.android;

import com.alibaba.fastjson.JSON;
import com.Model.Pojo.MoneyList;
import com.Model.DengLuModel.PanDuanZhangHao;
import com.Model.search.Search;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class SearchServlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
    	response.setCharacterEncoding("UTF-8");
    	PrintWriter out = response.getWriter();

    	String name = request.getParameter("username");
		String pass = request.getParameter("password");
		String shenfen = request.getParameter("shenfen");

		//实例化判断账号密码的对象
		PanDuanZhangHao panduan = new PanDuanZhangHao(name, pass, shenfen);
		//判断账号密码正确并获取返回值
		String flag = panduan.panDuan();
		String result;
		if(flag.equals("false")){
			result = flag;
		}else{
			Search se = new Search();
			ArrayList<MoneyList> moneylist = new ArrayList<MoneyList>();
			moneylist = se.getVal(name);
			result = JSON.toJSON(moneylist).toString();
		}
		
		out.print(result);
		
		out.flush();
		out.close();
	}

}
